<?php
/**
 * Created by PhpStorm.
 * User: HP
 * Date: 2021/5/18
 * Time: 10:32
 */

namespace app\backend\controller\v1;

use app\backend\validate\DoctorValidate;
use app\BaseController;
use app\common\model\Doctor as DoctorModel;
use think\exception\ValidateException;
use think\facade\Filesystem;

class Doctor extends BaseController
{
    //整形医生
    //曹文娜
    //显示
    public function index()
    {
        $model = new DoctorModel();
        $page = input('page/d', 1);
        $title = input('title');
        $where = [];
        if (!empty($title)) {
            $where[] = [
                ['title', 'like', "%$title%"],
            ];
        }
        $class = $model
            ->where($where)
            ->page($page, 5)
            ->select();
        $count = $model
            ->where($where)
            ->count();
        $maxpage = ceil($count / 5);
        return sucessMsg(200, '获取成功！', ['class' => $class, 'maxpage' => $maxpage]);
    }

    //添加
    public function create()
    {
        $user = new DoctorModel();
        $data = input();
        $data['image'] = delYuming($data['image']);
        try {
            validate(DoctorValidate::class)->scene('create')->check($data);
            if ($user->save($data)) {
                return sucessMsg(200, '创建成功！', $user->id);
            }
            return errMsg(500, '创建失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    //修改
    public function update()
    {
        $id = input('id/d');
        $user = DoctorModel::find($id);
        $data = input();
        $data['image'] = delYuming($data['image']);
        try {
            validate(DoctorValidate::class)->scene('update')->check($data);
            if (DoctorModel::update($data, $id)) {
                return sucessMsg(200, '修改成功！', $user->id);
            }
            return errMsg(500, '修改失败！');
        } catch (ValidateException $e) {
            return errMsg(400, $e->getError());
        }
    }

    //删除
    public function delete()
    {
        $data = input();
        $model = DoctorModel::where('id', $data['id'])->delete();
        if ($model) {
            return sucessMsg(200, '删除成功！');
        } else {
            return errMsg(500, '删除失败！');
        }
    }

}